package com.gxuwz.icefrede.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.icefrede.entity.Orders;
import com.gxuwz.icefrede.entity.User;
import com.gxuwz.icefrede.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 创建订单
     * @param orders
     * @return
     */
    @RequestMapping("/create")
    public String create(@RequestBody Orders orders, HttpSession session) {
        // 获取当前用户
        User user = (User) session.getAttribute("user");
        if (user == null){
            return "请先登录";
        } else if (user.getRole().equals("USER")) {
            // 设置订单日期
            orders.setOrderStatus("Processing");
            orders.setDeliveryId(0);
            orders.setUserId(user.getId());
            ordersService.save(orders);
            return  "订单创建成功!";
        }else {
            return null;
        }
    }

    /**
     * 用户删除订单
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Long  id) {
        System.out.println("id:" + id);
        boolean success = ordersService.removeById(id);
        System.out.println("success:" +success);
        if (success){
            return "success";
        }
        return null;
    }

    /**
     * 用户取消订单
     * @param id
     * @return
     */
    @RequestMapping("/cancel")
    public String cancelled(Long id) {
        System.out.println("orders:" + id);
        Orders order = ordersService.getById(id);
        if (id == null || order == null){
            return null;
        }else {
            order.setOrderStatus("Cancelled");
            System.out.println(order);
            ordersService.updateById(order);
            return "success";
        }
    }

    /**
     * 用户的订单列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/getMyOrders")
    public IPage<Orders> getMyOrders(int pageNum, int pageSize, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null){
            return null;
        }else if (user.getRole().equals("ADMIN")) {
            return ordersService.page(new Page<>(pageNum, pageSize));
        }else {
            LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Orders::getUserId, user.getId());
            return ordersService.page(new Page<>(pageNum, pageSize), wrapper);
        }
    }

    /**
     * 获取订单列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/getOrders")
    public IPage<Orders> getOrders(int pageNum, int pageSize) {
        return ordersService.page(new Page<>(pageNum, pageSize));
    }

    /**
     * 配送员获取用户订单列表
     * @param pageNum
     * @param pageSize
     * @param deliveryId
     * @return
     */
    @RequestMapping("/getOrdersByUserId")
    public IPage<Orders> getOrdersByUserId(int pageNum, int pageSize,long deliveryId) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getDeliveryId,deliveryId).eq(Orders::getOrderStatus,"Delivered");
        return ordersService.page(new Page<>(pageNum, pageSize),wrapper);
    }

    /**
     * 根据配送员id获取订单信息
     * @param pageNum
     * @param pageSize
     */
    @RequestMapping("/getOrdersByDeliveryId")
    public IPage<Orders> getOrdersByDeliveryId(int pageNum, int pageSize) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getDeliveryId,0).eq(Orders::getOrderStatus,"Processing");
        return ordersService.page(new Page<>(pageNum, pageSize),wrapper);
    }

    /**
     * 配送员接单
     * 配送员取消订单
     * @param orders
     * @return
     * @return
     */
    @RequestMapping("/update")
    public String update(@RequestBody Orders orders, HttpSession session){
        Orders order = ordersService.getById(orders.getId());
        User user = (User) session.getAttribute("user");
        if (order == null || user == null){
            return null;
        }else if(order.getOrderStatus().equals("Delivered")){  //取消订单
            order.setOrderStatus("Processing");
            order.setUserId(0);
            order.setDeliveryId(0);
            ordersService.updateById(order);
            System.out.println(order);
            return "success";
        }else {
            order.setOrderStatus("Delivered");               //接单
            order.setDeliveryId(orders.getDeliveryId());
            ordersService.updateById(order);
            return "success";
        }
    }

    /**
     * 配送员订单完成
     * @param orders
     * @return
     */
    @RequestMapping("/finish")
    public String finish(@RequestBody Orders orders){
        Orders order = ordersService.getById(orders.getId());
        if (order == null){
            return null;
        }else {
            order.setOrderStatus("Succeed");
            ordersService.updateById(order);
            return "success";
        }
    }
}
